package com.ali.juejin.ui.fragment.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.ali.juejin.BR
import com.ali.juejin.R
import com.ali.juejin.adapter.HomeRecommendArticleAdapter
import com.ali.juejin.adapter.HomeRecommendTagsAdapter
import com.ali.juejin.base.BaseFragment
import com.ali.juejin.model.main.HotListModel
import com.ali.juejin.widget.TagListView
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.lxj.xpopup.interfaces.XPopupCallback
import kotlinx.android.synthetic.main.fragment_home_hotlist.*

class CateListFragment: BaseFragment() {

    val model by lazy { ViewModelProvider(this).get(HotListModel::class.java) }

    val tagsAdapter by lazy { HomeRecommendTagsAdapter(context!!, BR.itemTag,model) }
    val articleAdapter by lazy { HomeRecommendArticleAdapter(context!!) }

    val builder by lazy { XPopup.Builder(context) }



    fun arrowRotate(){
        if(hotlist_show_tag_list.rotation == 90f){
            hotlist_show_tag_list.animate().rotation(270f).setDuration(500).start()
        }else{
            hotlist_show_tag_list.animate().rotation(90f).setDuration(500).start()
        }
    }

    override fun initView() {

        builder.setPopupCallback(object: XPopupCallback {
            override fun onBackPressed(): Boolean {
                return false
            }

            override fun onDismiss() {
                arrowRotate()
            }

            override fun beforeShow() {
            }

            override fun onCreated() {
            }

            override fun onShow() {
            }

        })


        // 标签
        hotlist_tags_recycler.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.HORIZONTAL,false)
        hotlist_tags_recycler.adapter = tagsAdapter


        // 文章
        hotlist_hot_article.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.VERTICAL,false)
        hotlist_hot_article.adapter = articleAdapter

        hotlist_scroll_con.setOnRefreshListener {
            model.getHomeRecommendList()
            it.finishRefresh(1000)
        }

        hotlist_scroll_con.setOnLoadMoreListener {
            model.getMoreHomeRecommendList()
            it.finishLoadMore(1000)
        }

        hotlist_show_tag_list.setOnClickListener {
            arrowRotate()

            builder.popupPosition(PopupPosition.Bottom).atView(hotlist_pop_con).asCustom(
                TagListView(context!!,model,tagsAdapter)
            ).show()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return View.inflate(context, R.layout.fragment_home_hotlist,container)
    }

    override fun initData() {
        model.getHomeRecommendList()

        model.homeRecommendList.observe(this, Observer {
                newValue->
            articleAdapter.load(newValue.data)
            tagsAdapter.load()
        })

        model.selectedTag.observe(this, Observer {
                _->
            hotlist_scroll_con.autoRefresh()
        })

    }

}